package com.myapi.client;

import java.util.ArrayList;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ProgressBar;
import com.extjs.gxt.ui.client.widget.Slider;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.myapi.client.DAO.CardDAO;
import com.myapi.client.DAO.LoginDAO;
import com.myapi.client.DAO.TableDAO;

public class TableUI extends AbsolutePanel {  

	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
		+ "attempting to contact the server. Please check your network "
		+ "connection and try again.";

	/**
	 * Create a remote service xy to talk to the server-side Greeting service.
	 */
	private final PokerServiceAsync pokerService = GWT
	.create(PokerService.class);

	private VerticalPanel mainPanel = new VerticalPanel();
	private FlexTable stocksFlexTable = new FlexTable();
	private HorizontalPanel addPanel = new HorizontalPanel();
	private TextBox newSymbolTextBox = new TextBox();
	private Button addStockButton = new Button("Add");
	private Label lastUpdatedLabel = new Label();
	private ArrayList<String> stocks = new ArrayList<String>();
	private static final int REFRESH_INTERVAL = 500; // ms

	private TextBox textbox=new TextBox();
	TextBox txtb_TxtChat;
	private TableDAO tableDAO=new TableDAO();
	private String myId="";
	private Image img_Player1Card2;
	private Image img_MyCard1;
	private Image img_MyCard2;
	private Image img_Player3Card2;
	private Image img_Player3Card1;
	private Image img_Player2Card2;
	private Image img_Player2Card1;
	private Image img_Player1Card1;
	private Image img_Player4Card2;
	private Image img_Player4Card1;
	private Image img_Player2;
	private Label lbl_namePlayer2;
	private Label lbl_chipsPlayer2;
	private Image img_Player3;
	private Label lbl_namePlayer3;
	private Label lbl_chipsPlayer3;
	private Image img_Player4;
	private Label lbl_namePlayer4;
	private Label lbl_chipsPlayer4;
	private Image img_Player1;
	private Label lbl_namePlayer1;
	private Label lbl_chipsPlayer1;
	private Label lbl_chipsTable;
	private Image img_CardFlop1;
	private Image img_CardFlop2;
	private Image img_CardFlop3;
	private Image img_CardTurn;
	private Image img_CardRiver;
	private TextArea txt_forum;
	private HorizontalPanel panel_Cards;
	private ProgressBar progressBar;
	private Button btn_Fold;
	private Button btn_CheckCall;
	private Button btn_Raise;
	private Button btn_LeaveTable;
	Boolean activeTimer=false;
	double CurrentBetAmout=0.0;

	boolean preflop=false;
	boolean flop=false;
	boolean turn=false;
	boolean river=false;


	int TIMEMAXPLAY=15;
	Timer timerPlayer;
	int timeCurrentPlayer;



	private LoginDAO loginDAO = null;
	private VerticalPanel loginPanel = new VerticalPanel();
	private Label loginLabel = new Label("Please sign in to your Google Account to access the Poker application.");
	private Anchor signInLink = new Anchor("Sign In");
	private Anchor signOutLink = new Anchor("Sign Out");

	private Slider slide;

	private TextBox txt_Raise;






	public TableUI(LoginDAO login) {
		// TODO Auto-generated constructor stub
		super();
		loginDAO=login;
		loadPokerTableUI();

		Timer t=new Timer(){

			@Override
			public void run() {
				update();

			}

		};
		t.scheduleRepeating(1000);

	}


	/**
	 * Test if a Card belong to the Common Card
	 * @param listCard List of commun card
	 * @param card Card to compare
	 * @return Return True if the card is in the commun card
	 */
	public boolean containsCard(ArrayList<CardDAO> listCard,CardDAO card){
		for(int i=0;i<listCard.size();i++){
			CardDAO card_tmp=listCard.get(i);

			if((card_tmp.getSuit().equals(card.getSuit())&&card_tmp.getValue()==card.getValue()))
				return true;
		}
		return false;

	}


	/**
	 * Fetch all information from the server
	 */
	public void update(){
		pokerService.update(new AsyncCallback<TableDAO>() {
			public void onFailure(Throwable caught) {
				System.out.println("Failure on c pas pourkoi");
			}


			@Override
			public void onSuccess(TableDAO tab) {

				if(tab==null)
					return;
				String chat="";
				if(tab.getForum().size()!=tableDAO.getForum().size()){
					for(int i=0;i<tab.Forum.size();i++)
						chat=tab.getForum().get(i)+"\n"+chat;
					txt_forum.setValue(chat);
				}

				tableDAO=tab;

				//txt_forum.setEmptyText(chat);
				//txt_forum.setEmptyText(myId+"\n Current "+tableDAO.currentPlayer.getId());

				/**
				 * For each PlayerHand we keep only cards which aren't in the common cards 
				 */
				if(tableDAO.gameRunning){
					if(!tableDAO.currentPlayer.getId().equals(loginDAO.getEmailAddress())){
						btn_Fold.setEnabled(false);
						stopTimer("");
						btn_Raise.setEnabled(false);
						btn_CheckCall.setEnabled(false);
						activeTimer=false;
						slide.setValue(0);
						if(slide.hasListeners()){
							slide.removeAllListeners();
						}


					}
					else {
						btn_Raise.setEnabled(true);
						btn_Fold.setEnabled(true);
						btn_CheckCall.setEnabled(true);
						slide.setMaxValue((int) (tableDAO.currentPlayer.getChips()));
						if(tableDAO.getCurrentBet()!=0.0){
							btn_CheckCall.setText("Call :"+(tableDAO.getCurrentBet()-CurrentBetAmout));
						}
						else
							btn_CheckCall.setText("Check");
						startTimer();
						slide.addListener(Events.Change,new Listener(){

							@Override
							public void handleEvent(BaseEvent be) {


								int bet=slide.getValue();

								if(slide.getMaxValue()==bet){
									txt_Raise.setText(""+tableDAO.getCurrentPlayer().getChips());
									btn_Raise.setText("All in");
								}
								else{
									btn_Raise.setText("Raise");
									txt_Raise.setText(""+bet);
								}





							}

						});

					}
					updatePlayerHand(tableDAO);
					updateCommCard(tableDAO);
				}

				else
				{
					btn_Fold.setEnabled(false);	
					btn_CheckCall.setEnabled(false);
					btn_LeaveTable.setEnabled(true);

					initRound();



				}
				updatePlayerInfo(tableDAO);

			}

		});

	}
	/**
	 * Check if the bet is higher than the currentbet of the other player
	 * We take in consideration ower last bet if we are in the same turn
	 * @param bet Amout of the current bet
	 * @return
	 */
	public boolean CheckEnoughBet(int bet){
		return (bet>(tableDAO.getCurrentBet()-CurrentBetAmout));
	}

	/**
	 * Init for a new round, All the card show the back picture
	 */
	public void initRound(){

		preflop=false;
		flop=false;
		turn=false;
		river=false;
		btn_Raise.setText("Raise");
		String img_back="Cards/back_blue.png";
		img_Player1Card1.setUrl(img_back);
		img_Player1Card2.setUrl(img_back);

		img_Player2Card1.setUrl(img_back);
		img_Player2Card2.setUrl(img_back);

		img_Player3Card1.setUrl(img_back);
		img_Player3Card2.setUrl(img_back);


		img_Player4Card1.setUrl(img_back);
		img_Player4Card2.setUrl(img_back);
		img_CardFlop1.setUrl(img_back);
		img_CardFlop2.setUrl(img_back);
		img_CardFlop3.setUrl(img_back);
		img_CardTurn.setUrl(img_back);

		img_CardRiver.setUrl(img_back);

	}
	/**
	 * Show the player hand at the beginning of the game
	 * @param tableDAO
	 */
	private void updatePlayerHand(TableDAO tableDAO) {
		//We show the player hand at the beginning of the game
		if(!preflop){
			//Each hand contain 7 cards but we only want the 2 from the each player
			int numCardHand=0;
			String [][] urlCard=new String[tableDAO.getPlayerHands().size()][2];
			for(int j=0;j<tableDAO.getPlayerHands().size();j++){
				ArrayList<CardDAO> playersCards=tableDAO.getPlayerHands().get(j).getCards();
				for(int i=0;i<playersCards.size();i++){
					if(!containsCard(tableDAO.getCommCards(),playersCards.get(i))){
						urlCard[j][numCardHand]="Cards/"+playersCards.get(i).getValue()+playersCards.get(i).getSuit()+".png";
						numCardHand++;
					}

				}
				numCardHand=0;

			}


			/**
			 * Update Players Hand
			 */
			if(tableDAO.getPlayerHands().size()>=1){
				PlayerHandDAO playerHandDAO=tableDAO.getPlayerHands().get(0);
				ArrayList<CardDAO> playersCards=playerHandDAO.getCards();

				img_Player1Card1.setUrl(urlCard[0][0]);
				img_Player1Card2.setUrl(urlCard[0][1]);

				if(tableDAO.getPlayerHands().size()>=2){
					playerHandDAO=tableDAO.getPlayerHands().get(1);
					playersCards=playerHandDAO.getCards();

					img_Player2Card1.setUrl(urlCard[1][0]);
					img_Player2Card2.setUrl(urlCard[1][1]);

					if(tableDAO.getPlayerHands().size()>=3){
						playerHandDAO=tableDAO.getPlayerHands().get(2);
						playersCards=playerHandDAO.getCards();

						img_Player3Card1.setUrl(urlCard[2][0]);
						img_Player3Card2.setUrl(urlCard[2][1]);

						if(tableDAO.getPlayerHands().size()>=4){
							playerHandDAO=tableDAO.getPlayerHands().get(3);
							playersCards=playerHandDAO.getCards();

							img_Player4Card1.setUrl(urlCard[3][0]);
							img_Player4Card2.setUrl(urlCard[3][1]);
						}
					}
				}
			}
			preflop=true;
		}



	}


	/**
	 * Update Player Information : Chips , name
	 * @param tableDAO Information about the table
	 */
	private void updatePlayerInfo(TableDAO tableDAO) {


		if(tableDAO.getPlayerHands().size()>=1){
			PlayerHandDAO playerHandDAO=tableDAO.getPlayerHands().get(0);
			ArrayList<CardDAO> playersCards=playerHandDAO.getCards();
			lbl_namePlayer1.setText(""+playerHandDAO.getPlayerInfo().getName()+" Chips :" +playerHandDAO.getPlayerInfo().getChips());
			lbl_chipsPlayer1.setText(""+tableDAO.getActivePot().getAllPlayerContributions(playerHandDAO.getPlayerInfo()));

			if(tableDAO.getPlayerHands().size()>=2){
				playerHandDAO=tableDAO.getPlayerHands().get(1);
				playersCards=playerHandDAO.getCards();

				lbl_namePlayer2.setText(""+playerHandDAO.getPlayerInfo().getName()+" Chips :" +playerHandDAO.getPlayerInfo().getChips());
				lbl_chipsPlayer2.setText(""+tableDAO.getActivePot().getAllPlayerContributions(playerHandDAO.getPlayerInfo()));

				if(tableDAO.getPlayerHands().size()>=3){
					playerHandDAO=tableDAO.getPlayerHands().get(2);
					playersCards=playerHandDAO.getCards();

					lbl_namePlayer3.setText(""+playerHandDAO.getPlayerInfo().getName()+" Chips :" +playerHandDAO.getPlayerInfo().getChips());
					lbl_chipsPlayer3.setText(""+tableDAO.getActivePot().getAllPlayerContributions(playerHandDAO.getPlayerInfo()));

					if(tableDAO.getPlayerHands().size()>=4){
						playerHandDAO=tableDAO.getPlayerHands().get(3);
						playersCards=playerHandDAO.getCards();

						lbl_namePlayer4.setText(""+playerHandDAO.getPlayerInfo().getName()+" Chips :" +playerHandDAO.getPlayerInfo().getChips());
						lbl_chipsPlayer4.setText(""+tableDAO.getActivePot().getAllPlayerContributions(playerHandDAO.getPlayerInfo()));
					}
				}
			}


		}
	}


	/**
	 * Update image for the Common card
	 */
	private void updateCommCard(TableDAO tableDAO) {


		if(tableDAO.getCommCardCounter()>=3&&!flop){
			flop=true;
			//reinitialise the current amout for a new turn
			CurrentBetAmout=tableDAO.getActivePot().getAllPlayerContributions(tableDAO.getCurrentPlayer());

			img_CardFlop1.setUrl("Cards/"+tableDAO.getCommCards().get(0).getValue()+tableDAO.getCommCards().get(0).getSuit()+".png");
			img_CardFlop2.setUrl("Cards/"+tableDAO.getCommCards().get(1).getValue()+tableDAO.getCommCards().get(1).getSuit()+".png");
			img_CardFlop3.setUrl("Cards/"+tableDAO.getCommCards().get(2).getValue()+tableDAO.getCommCards().get(2).getSuit()+".png");
		}
		if(tableDAO.getCommCardCounter()>=4&&!turn&&flop){
			turn=true;
			//reinitialise the current amout for a new turn
			CurrentBetAmout=0;
			img_CardTurn.setUrl("Cards/"+tableDAO.getCommCards().get(3).getValue()+tableDAO.getCommCards().get(3).getSuit()+".png");
		}
		if(tableDAO.getCommCardCounter()>=5&&(!river)&&turn&&flop){
			river=true;
			//reinitialise the current amout for a new turn
			CurrentBetAmout=0;
			img_CardRiver.setUrl("Cards/"+tableDAO.getCommCards().get(4).getValue()+tableDAO.getCommCards().get(4).getSuit()+".png");
		}


	}
	/**
	 *  Start Game
	 */


	public void startTimer(){
		if(!activeTimer){
			progressBar.updateProgress(0," start");
			activeTimer=true;
			timeCurrentPlayer=0;
			timerPlayer = new Timer() {


				@Override
				public void run() {
					timeCurrentPlayer=timeCurrentPlayer+1;
					progressBar.updateProgress((double)(timeCurrentPlayer/50.0),"");
					if(timeCurrentPlayer==50){
						progressBar.updateProgress(1,"Automatic Fold : too late");
						timeCurrentPlayer=0;
						timerPlayer.cancel();


						//We fold automaticily after the timeout
						pokerService.Fold(loginDAO.getEmailAddress(),new AsyncCallback(){

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}


							@Override
							public void onSuccess(Object result) {
								stopTimer("Automatic Fold : too late");
							}
						});
					}
				}
			};

			timerPlayer.scheduleRepeating(1000);
		}
	}

	/**
	 * Stop Progress Bar
	 */
	public void stopTimer(String action){
		timerPlayer.cancel();
		activeTimer=false;
		progressBar.updateProgress(0, action);

	}
	/**
	 * Create the UI for the Poker Table
	 */
	public void loadPokerTableUI(){
		// Set up sign out hyperlink.
		//  signOutLink.setHref(loginDAO.getLogoutUrl());
		new AbsolutePanel();
		setSize("780", "610");
		Image image = new Image("Cards/TableRT.png");
		add(image, 70, 29);
		image.setSize("632px", "429px");



		HorizontalPanel panel_SouthPanel = new HorizontalPanel();
		add(panel_SouthPanel, 52, 424);
		panel_SouthPanel.setSize("662px", "144px");

		VerticalPanel panel_Info = new VerticalPanel();
		panel_Info.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel_Info.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel_Info.setSize("97px", "142px");
		panel_SouthPanel.add(panel_Info);

		btn_LeaveTable = new Button("Leave");
		btn_LeaveTable.setText("Play");
		btn_LeaveTable.setSize("77", "34");
		panel_Info.add(btn_LeaveTable);


		Button btn_Statistic = new Button("Statistic");
		panel_Info.add(btn_Statistic);
		btn_Statistic.setSize("77", "34");


		Button btn_LogOut = new Button("Log out");
		panel_Info.add(btn_LogOut);
		btn_LogOut.setSize("77", "34");



		VerticalPanel panel_Action = new VerticalPanel();
		panel_Action.setSize("297px", "144px");
		panel_SouthPanel.add(panel_Action);

		progressBar = new ProgressBar();
		panel_Action.add(progressBar);


		HorizontalPanel panel_FCR = new HorizontalPanel();
		panel_FCR.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel_FCR.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel_Action.add(panel_FCR);
		panel_FCR.setSize("401px", "42px");

		btn_Fold = new Button("Fold");
		panel_FCR.add(btn_Fold);
		btn_Fold.setSize("77", "34");


		btn_CheckCall = new Button("Check");
		panel_FCR.add(btn_CheckCall);
		btn_CheckCall.setSize("77", "34");


		btn_Raise = new Button("Raise");
		panel_FCR.add(btn_Raise);
		btn_Raise.setSize("77", "34");

		HorizontalPanel panel_Raise = new HorizontalPanel();
		panel_Raise .setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel_Raise .setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		slide =new Slider();
		slide.setSize("300", "20");

		slide.setIncrement(1);  
		slide.setMaxValue(200);

		panel_Raise.add(slide);

		txt_Raise=new TextBox();
		txt_Raise.setSize("30", "30");
		panel_Raise.add(txt_Raise);

		panel_Action.add(panel_Raise);

		VerticalPanel panel_ChatPanel = new VerticalPanel();
		panel_ChatPanel.setSize("199px", "143px");
		panel_SouthPanel.add(panel_ChatPanel);

		txt_forum = new TextArea();
		panel_ChatPanel.add(txt_forum);
		txt_forum.setSize("194px", "99px");


		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		horizontalPanel_1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel_1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel_ChatPanel.add(horizontalPanel_1);
		horizontalPanel_1.setSize("186px", "43px");

		txtb_TxtChat = new TextBox();
		horizontalPanel_1.add(txtb_TxtChat);
		txtb_TxtChat.setSize("126px", "31px");


		Button button_Send = new Button("Send");
		horizontalPanel_1.add(button_Send);
		button_Send.setSize("39px", "32px");






		panel_Cards = new HorizontalPanel();
		panel_Cards.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel_Cards.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		add(panel_Cards, 230, 170);
		panel_Cards.setSize("357px", "107px");

		img_CardFlop1 = new Image("Cards/1Diamond.png");
		panel_Cards.add(img_CardFlop1);
		img_CardFlop1.setSize("59px", "100px");


		img_CardFlop2 = new Image("Cards/1Diamond.png");
		panel_Cards.add(img_CardFlop2);
		img_CardFlop2.setSize("59px", "100px");


		img_CardFlop3 = new Image("Cards/1Diamond.png");
		panel_Cards.add(img_CardFlop3);
		img_CardFlop3.setSize("59px", "100px");


		img_CardTurn = new Image("Cards/4.png");
		panel_Cards.add(img_CardTurn);
		img_CardTurn.setSize("59px", "100px");


		img_CardRiver = new Image("Cards/5.png");
		panel_Cards.add(img_CardRiver);
		img_CardRiver.setSize("59px", "100px");



		img_MyCard1 = new Image("Cards/32.png");
		add(img_MyCard1, 402, 310);
		img_MyCard1.setSize("69px", "94px");


		img_MyCard2 = new Image("Cards/23.png");
		add(img_MyCard2, 433, 325);
		img_MyCard2.setSize("69px", "94px");


		img_Player3Card2 = new Image("Cards/32.png");
		add(img_Player3Card2, 528, 40);
		img_Player3Card2.setSize("69px", "94px");


		img_Player3Card1 = new Image("Cards/23.png");
		add(img_Player3Card1, 553, 55);
		img_Player3Card1.setSize("69px", "94px");


		img_Player2Card2 = new Image("Cards/32.png");
		add(img_Player2Card2, 104, 40);
		img_Player2Card2.setSize("69px", "94px");


		img_Player2Card1 = new Image("Cards/23.png");
		add(img_Player2Card1, 125, 61);
		img_Player2Card1.setSize("69px", "94px");


		img_Player1Card2 = new Image("Cards/32.png");
		add(img_Player1Card2, 104, 282);
		img_Player1Card2.setSize("69px", "94px");


		img_Player1Card1 = new Image("Cards/23.png");
		add(img_Player1Card1, 125, 307);
		img_Player1Card1.setSize("69px", "94px");


		img_Player4Card2 = new Image("Cards/32.png");
		add(img_Player4Card2, 625, 282);
		img_Player4Card2.setSize("69px", "94px");


		img_Player4Card1 = new Image("Cards/23.png");
		add(img_Player4Card1, 650, 294);
		img_Player4Card1.setSize("69px", "94px");


		HorizontalPanel panel_Player2 = new HorizontalPanel();
		panel_Player2.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel_Player2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		add(panel_Player2, 217, 5);
		panel_Player2.setSize("113px", "82px");

		VerticalPanel verticalPanel = new VerticalPanel();
		panel_Player2.add(verticalPanel);
		verticalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		verticalPanel.setSize("62px", "70px");

		img_Player2 = new Image((String) null);
		verticalPanel.add(img_Player2);
		img_Player2.setSize("42px", "43px");


		lbl_namePlayer2 = new Label("name2");
		verticalPanel.add(lbl_namePlayer2);
		lbl_namePlayer2.setSize("58px", "25px");



		lbl_chipsPlayer2 = new Label("Player2");
		panel_Player2.add(lbl_chipsPlayer2);
		lbl_chipsPlayer2.setSize("50px", "43px");



		HorizontalPanel panel_Player3 = new HorizontalPanel();
		panel_Player3.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel_Player3.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		add(panel_Player3, 410, 5);
		panel_Player3.setSize("113px", "82px");

		VerticalPanel verticalPanel3 = new VerticalPanel();
		verticalPanel3.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel_Player3.add(verticalPanel3);
		verticalPanel3.setSize("62px", "70px");

		img_Player3 = new Image((String) null);
		verticalPanel3.add(img_Player3);
		img_Player3.setSize("42px", "43px");


		lbl_namePlayer3 = new Label("name3");
		verticalPanel3.add(lbl_namePlayer3);
		lbl_namePlayer3.setSize("58px", "25px");



		lbl_chipsPlayer3 = new Label("Player3");
		panel_Player3.add(lbl_chipsPlayer3);
		lbl_chipsPlayer3.setSize("50px", "43px");



		HorizontalPanel panel_Player4 = new HorizontalPanel();
		panel_Player4.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel_Player4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		add(panel_Player4, 625, 179);
		panel_Player4.setSize("113px", "82px");

		VerticalPanel verticalPanel4 = new VerticalPanel();
		verticalPanel4.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel_Player4.add(verticalPanel4);
		verticalPanel4.setSize("62px", "70px");

		img_Player4 = new Image((String) null);
		verticalPanel4.add(img_Player4);
		img_Player4.setSize("42px", "43px");


		lbl_namePlayer4 = new Label("name4");
		verticalPanel4.add(lbl_namePlayer4);
		lbl_namePlayer4.setSize("58px", "25px");



		lbl_chipsPlayer4 = new Label("Player4");
		panel_Player4.add(lbl_chipsPlayer4);
		lbl_chipsPlayer4.setSize("50px", "43px");



		HorizontalPanel panel_Player1 = new HorizontalPanel();
		panel_Player1.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		panel_Player1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		add(panel_Player1, 17, 192);
		panel_Player1.setSize("113px", "82px");

		VerticalPanel verticalPanel1 = new VerticalPanel();
		verticalPanel1.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		panel_Player1.add(verticalPanel1);
		verticalPanel1.setSize("62px", "70px");

		img_Player1 = new Image((String) null);
		verticalPanel1.add(img_Player1);
		img_Player1.setSize("42px", "43px");


		lbl_namePlayer1 = new Label("name1");
		verticalPanel1.add(lbl_namePlayer1);
		lbl_namePlayer1.setSize("58px", "25px");



		lbl_chipsPlayer1 = new Label("Player1");
		panel_Player1.add(lbl_chipsPlayer1);
		lbl_chipsPlayer1.setSize("50px", "43px");



		lbl_chipsTable = new Label("Chips table");
		add(lbl_chipsTable, 336, 121);
		lbl_chipsTable.setSize("89px", "28px");

		initRound();



		/************************ Action *******************************************/


		btn_LeaveTable.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				pokerService.addPlayer(loginDAO.getNickname(),loginDAO.getEmailAddress(), new AsyncCallback(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}


					@Override
					public void onSuccess(Object result) {
						btn_LeaveTable.setEnabled(false);

					}
				});
				update();

			}
		});

		button_Send.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				pokerService.addMessage(txtb_TxtChat.getText(),new AsyncCallback(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}


					@Override
					public void onSuccess(Object result) {
						txtb_TxtChat.setText("");

					}
				});
				update();

			}
		}
		);

		btn_Raise.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				double bet=(double)Double.parseDouble(txt_Raise.getText());
				pokerService.Bet(tableDAO.getCurrentBet()+bet, loginDAO.getEmailAddress(),new AsyncCallback(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}


					@Override
					public void onSuccess(Object result) {
						stopTimer("Raise");
						update();


					}
				});


			}
		}
		);
		btn_CheckCall.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override

			public void componentSelected(ButtonEvent ce) {

				pokerService.Call(loginDAO.getEmailAddress(),new AsyncCallback(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}


					@Override
					public void onSuccess(Object result) {
						stopTimer("Call");
						update();
					}
				});
				update();
			}
		}
		);

		btn_Fold.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {

				pokerService.Fold(loginDAO.getEmailAddress(),new AsyncCallback(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}


					@Override
					public void onSuccess(Object result) {
						pokerService.getStatistic(tableDAO.getCurrentPlayer(),new AsyncCallback(){

							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub

							}
							@Override
							public void onSuccess(Object result) {}
						});

						stopTimer("Fold");
						update();

					}
				});
				update();
			}
		}
		);
		btn_Statistic.addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {

				pokerService.getInfoPokerOdd(new AsyncCallback(){

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}


					@Override
					public void onSuccess(Object result) {


					}
				});
				update();
			}
		}
		);

	}




}
